package com.runner.unlocks;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.runner.observer.Event;
import com.runner.observer.Subject;
import com.runner.utility.Constants;
import com.runner.utility.Locator;

public class MultUnlock implements Constants{

	public static final int MULT_AMOUNT=50;
	private ImageButton button;
	private Subject subject;
	private Label name,unlockField;
	private Image coinImage;
	private boolean bought;
	
	public MultUnlock(Stage stage, LabelStyle style, Subject subject){
		this.subject=subject;
		createButton(stage,style);
	}
	
	public void createButton(Stage stage, LabelStyle style){
		Image i=new Image(Locator.getAssets().get(MULT_PICTURE, Texture.class));
		button=new ImageButton(i.getDrawable());
		button.addListener(new ClickListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				if(!bought){
					unlock();
				}
				
				return false;
			}
		});
		button.setPosition(ACTUAL_VIRTUAL_WIDTH * (RINGER_X+SEPARATION_X*2), ACTUAL_VIRTUAL_HEIGHT * (BOOST_Y+ HEIGHT_ADJUST));
		
		name=new Label("Coin Multiplier",style);
		name.setPosition(ACTUAL_VIRTUAL_WIDTH * (RINGER_X +SEPARATION_X*2+ 0.009f), ACTUAL_VIRTUAL_HEIGHT * (BOOST_Y + 0.168f+ HEIGHT_ADJUST));
		
		coinImage = new Image(Locator.getAssets().get(SINGLE_COIN_PATH, Texture.class));
		coinImage.setPosition(ACTUAL_VIRTUAL_WIDTH * (RINGER_X + SEPARATION_X*2+ 0.10f), ACTUAL_VIRTUAL_HEIGHT * (BOOST_Y + 0.015f+ HEIGHT_ADJUST));
		coinImage.setScale((1 ) * 0.57F);
		
		unlockField = new Label("Use for "+MULT_AMOUNT, style);
		unlockField.setPosition(ACTUAL_VIRTUAL_WIDTH * (RINGER_X + SEPARATION_X*2+ 0.01f), ACTUAL_VIRTUAL_HEIGHT * (BOOST_Y + 0.01f+ HEIGHT_ADJUST));
		
		stage.addActor(coinImage);
		stage.addActor(unlockField);
		stage.addActor(name);
		stage.addActor(button);
	}
	
	public void unlock() {
		if (Locator.getCoins() >= MULT_AMOUNT) {
			unlockField.setVisible(false);
			coinImage.setVisible(false);
			bought=true;
			Locator.removeCoins(MULT_AMOUNT);
			subject.notify(Event.EVENT_MULT_SELECTED);
		}
	}

	public void reset(){
		bought=false;
		unlockField.setVisible(true);
		coinImage.setVisible(true);
	}

}
